# 从sklearn.datasets导入 iris数据加载器
from sklearn.datasets import load_iris
# 从sklearn.cross_validation里导入train_test_split用于数据分割
from sklearn.cross_validation import train_test_split
# 从sklearn.preprocessing里选择导入数据标准化模块
from sklearn.preprocessing import StandardScaler
# 从sklearn.neighbors里选择导入KNeighborsClassifier，即K近邻分类器
from sklearn.neighbors import KNeighborsClassifier
# 从sklearn.metrics引入classification_report模块对预测结果做更加详细的分析
from sklearn.metrics import classification_report

# from sklearn.externals import joblib

# 1）使用加载器读取数据并且存入变量iris
iris = load_iris()

# 2）从使用train_test_split利用随机种子random_state采样25%的数据作为测试集
X_train,X_test, y_train, y_test =  train_test_split(iris.data, iris.target, test_size=0.25, random_state=33)

# 3）对训练集和测试集的特征数据进行标准化
ss = StandardScaler()
X_train = ss.fit_transform(X_train)
X_test = ss.transform(X_test)

# 4）使用K近邻分类器对测试数据进行类别预测，预测结果存储在变量y_predict中
knc = KNeighborsClassifier()
knc.fit(X_train, y_train)
y_predict = knc.predict(X_test)
# print(y_predict)
 
# 5) 做准确率判断及用classification_report模块对预测结果做更加详细的分析
print('使用模型自带的评估函数进行准确率判断:', knc.score(X_test, y_test))
print(classification_report(y_test, y_predict, target_names=iris.target_names))

